package com.bjqh.edu.UI.Activity.StudentPersonal;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bjqh.edu.Base.AppConstants;
import com.bjqh.edu.Bean.OrderDetail;
import com.bjqh.edu.Bean.Orderdetails;
import com.bjqh.edu.R;
import com.bjqh.edu.UI.Activity.Login.LoginActivity;
import com.bjqh.edu.UI.dialog.ConfirmDialog;
import com.bjqh.edu.UI.dialog.LoadDialog;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

public class AlreadyDetailsActivity extends AppCompatActivity {

    @BindView(R.id.iv_img)
    ImageView ivImg;
    @BindView(R.id.tv_des)
    TextView tvDes;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_count)
    TextView tvCount;
    @BindView(R.id.btn_payment)
    Button btnPayment;
    @BindView(R.id.pop_layout)
    LinearLayout popLayout;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_qx)
    TextView tvQx;
    @BindView(R.id.rl_top_tilte)
    RelativeLayout rlTopTilte;
    Orderdetails.DataBean dataBean;
    OrderDetail.OrderBean orderBean;
    OrderDetail.OrderBean.CourseBean courseBean;
    private LoadDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_already_details);
        ButterKnife.bind(this);
        Bundle bundle = (Bundle) getIntent().getExtras().get("bundle");
        dataBean = (Orderdetails.DataBean) bundle.getParcelable("data");
        dialog = new LoadDialog(this,"火速加载中...");
        dialog.initDialog(this,"火速加载中...");
        OkGo.post(AppConstants.BASE_URL + "/order/detail.shtml")
                .tag(this)
                .cacheKey("cacheKey")
                .cacheMode(CacheMode.DEFAULT)
                .params("id", dataBean.getId())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        LoadDialog.closeDialog();
                        Log.e("canceled", s);
                        Gson gson = new Gson();
                        OrderDetail orderDetail = gson.fromJson(s, OrderDetail.class);
                        if (orderDetail.getMsg() != null) {
                            if (orderDetail.getMsg().equals("timeout")) {
                                AppConstants.gotoLogin(AlreadyDetailsActivity.this);
                            } else if (orderDetail.getMsg().equals("remote login")) {
                                AppConstants.remologin(AlreadyDetailsActivity.this);
                            } else if (orderDetail.getMsg().equals("ac none")) {
                                btnPayment.setText("课程已售完");
                                btnPayment.setBackgroundColor(getResources().getColor(R.color.black_gray));
                                btnPayment.setEnabled(false);
                                orderBean = orderDetail.getOrder();
                                if (orderBean.getCourse() != null) {
                                    courseBean = orderBean.getCourse();
                                    Glide.with(getApplication())
                                            .load(AppConstants.IMG_URL + courseBean.getImg())
                                            .asBitmap()
                                            .placeholder(R.mipmap.iv_err)
                                            .into(ivImg);
                                }
                            } else if (orderDetail.getMsg().equals("ac end")) {
                                orderBean = orderDetail.getOrder();
                                if (orderBean.getCourse() != null) {
                                    btnPayment.setText("活动已结束");
                                    btnPayment.setBackgroundColor(getResources().getColor(R.color.black_gray));
                                    btnPayment.setEnabled(false);
                                    courseBean = orderBean.getCourse();
                                    Glide.with(getApplication())
                                            .load(AppConstants.IMG_URL + courseBean.getImg())
                                            .asBitmap()
                                            .placeholder(R.mipmap.iv_err)
                                            .into(ivImg);
                                }
                            } else {
                                orderBean = orderDetail.getOrder();
                                if (orderBean.getCourse() != null) {
                                    courseBean = orderBean.getCourse();
                                    Glide.with(getApplication())
                                            .load(AppConstants.IMG_URL + courseBean.getImg())
                                            .asBitmap()
                                            .placeholder(R.mipmap.iv_err)
                                            .into(ivImg);
                                }
                            }
                        }
                    }
                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        Toast.makeText(AlreadyDetailsActivity.this, "报告小主，服务器被外星人偷走啦~~", Toast.LENGTH_SHORT).show();
                        LoadDialog.closeDialog();
                    }
                });
        tvDes.setText(dataBean.getCoursedes());
        tvName.setText(dataBean.getCoursename() + "X1");
        tvCount.setText("￥" + String.valueOf(dataBean.getPrice()));
        tvQx.setVisibility(View.VISIBLE);
        tvTitle.setText("待支付");
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        tvQx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder normalDialog =
                        new AlertDialog.Builder(AlreadyDetailsActivity.this).
                                setTitle("您确定要取消订单么？")
                                .setPositiveButton("确定",
                                        new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                OkGo.post(AppConstants.BASE_URL + "/order/updateCorder.shtml")
                                                        .tag(this)
                                                        .cacheKey("cacheKey")
                                                        .cacheMode(CacheMode.DEFAULT)
                                                        .params("id", dataBean.getId())
                                                        .params("status", "4")
                                                        .execute(new StringCallback() {
                                                            @Override
                                                            public void onSuccess(String s, Call call, Response response) {
                                                                Log.e("取消订单", s);
                                                                finish();
                                                                startActivity(new Intent(AlreadyDetailsActivity.this, OrderActivity.class));
                                                            }
                                                            @Override
                                                            public void onError(Call call, Response response, Exception e) {
                                                            }
                                                        });
                                            }
                                        });
                normalDialog.setNegativeButton("取消",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        }).create();
                // 显示
                normalDialog.show();
            }
        });
        //设置按钮监听
        btnPayment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.putExtra("ordernum", orderBean.getOrdernum());
                intent.putExtra("price", String.valueOf(orderBean.getPrice()));
                intent.putExtra("coursedes", orderBean.getCoursedes());
                intent.putExtra("coursename", orderBean.getCoursename());
                intent.putExtra("cid", String.valueOf(orderBean.getCid()));
                intent.setClass(AlreadyDetailsActivity.this, PaymentActivity.class);
                AlreadyDetailsActivity.this.startActivity(intent);
                finish();
            }
        });
    }
}
